package googleDocs;


import java.io.File;
import java.net.URL;
import java.util.Vector;

import main.Main;

import com.google.gdata.client.docs.DocsService;
import com.google.gdata.data.PlainTextConstruct;
import com.google.gdata.data.docs.DocumentEntry;
import com.google.gdata.data.docs.DocumentListEntry;
import com.google.gdata.data.docs.DocumentListFeed;

public class UploadToGD {
	public static void main(String[] args) {
		try {
			String email = ""; 				//Vul eigen e-mail in
			String password = "";			//Vul eigen paswoord in
			String dbName = "test";			//Vul geldige database naam in
			Main.initConnection(dbName, "", "");
			/*uploadFile(email, password, dbName, "src/importexport/GDExport.txt");
			deleteFile(email, password, "document.txt");
			printGoogleDocsContent(getGoogleDocsContent(email, password));*/
			getGoogleDocsFile(email, password, "GDExport.txt");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private static String getDocId(String email, String password, String fileName) throws Exception {
		// geeft de document id van een bestand op googledocs weer
		DocsService service = new DocsService("Document list");
		service.setUserCredentials(email, password);
		URL documentlistFeedUrl = new URL ("http://docs.google.com/feeds/documents/private/full");
		DocumentListFeed feed = service.getFeed(documentlistFeedUrl, DocumentListFeed.class);
		String shortId = "";
		for (DocumentListEntry entry : feed.getEntries()) {
			if (compareStrings(entry.getTitle().getPlainText(), fileName)) {
				shortId = entry.getId().substring(entry.getId().lastIndexOf('/') + 1);;
			}
		}
		return shortId;
	}
	
	private static String getTypeFromFilename(String filename) {
		// geeft het type van een bestand weer
		return filename.substring(filename.lastIndexOf(".") + 1);
	} 
	
	public static void getGoogleDocsFile(String email, String password, String fileName) throws Exception {
		// haalt een bestand van google docs en zet het in de map 'googleDocs' 
		String objectId = getDocId(email, password, fileName); 
		String filePath = "src/GoogleDocs/" + fileName;
		DocumentList documentList = new DocumentList("Login");
		documentList.login(email, password);
		documentList.downloadFile(objectId, filePath, 
		DocumentList.getDownloadFormat(objectId, getTypeFromFilename(filePath)));
	}
	
	private static boolean compareStrings(String s1, String s2){
		// vergelijkt 2 Strings
		if (s1.length() != s2.length()){
			return false;
		} else {
			for (int i = 0; i<s1.length(); i++){
				if (s1.charAt(i) != s2.charAt(i)) {
					return false;
				}
			}
		}
		return true;
	}
	
	public static boolean deleteFile(String email, String password, String fileName) throws Exception{
		// verwijdert 'fileName' van google docs
		String temp = "";
		boolean found = false;
		DocsService service = new DocsService("Document list");
		service.setUserCredentials(email, password);
		URL documentlistFeedUrl = new URL ("http://docs.google.com/feeds/documents/private/full");
		DocumentListFeed feed = service.getFeed(documentlistFeedUrl, DocumentListFeed.class);
		
		for (DocumentListEntry entry : feed.getEntries()) {
			temp = entry.getTitle().getPlainText();
			if (compareStrings(temp, fileName)) {
				entry.delete();
				found = true;
			}
		}
		return found;
	}
	
	public static void uploadFile(String email, String password, String filePath) throws Exception {
		// upload file naar google docs
		DocsService service = new DocsService("Document List"); 
		service.setUserCredentials(email, password); 
		URL documentListFeedUrl = new URL("http://docs.google.com/feeds/documents/private/full"); 
		DocumentEntry newDocument = new DocumentEntry();
		File documentFile = new File(filePath);
		newDocument.setFile(documentFile);
		newDocument.setTitle(new PlainTextConstruct(documentFile.getName())); 
		DocumentListEntry uploaded = service.insert(documentListFeedUrl, newDocument);
	}
	
	public static Vector<String> getGoogleDocsContent(String email, String password) throws Exception{
		// returnt alle bestandsnamen die op google docs staan
		int counter = 0;
		Vector<String> v = new Vector<String>();
		DocsService service = new DocsService("Document list");
		service.setUserCredentials(email, password);
		URL documentlistFeedUrl = new URL ("http://docs.google.com/feeds/documents/private/full");
		DocumentListFeed feed = service.getFeed(documentlistFeedUrl, DocumentListFeed.class);

		for (DocumentListEntry entry : feed.getEntries()) {
			v.add(counter, entry.getTitle().getPlainText());
			counter++;
		}
		return v;
	}
	
	public static void printGoogleDocsContent(Vector<String> v) {
		for (int i = 0; i < v.size(); i++) {
			System.out.println(v.get(i));
		}
	}
}
